TARGET_DIR = pdf
SOURCES := $(wildcard ./*.tex)
OBJECTS := $(patsubst ./%.tex, $(TARGET_DIR)/%.pdf, $(SOURCES))

CC = latexmk
TEXFLAGS = -xelatex -shell-escape -file-line-error -synctex=1 -interaction=nonstopmode

MIDOBJS := *.aux *.fdb_latexmk *.fls *.log *.xdv *.synctex.gz *.synctex\(busy\) *.out *.toc *.lot *.lof *.pyg.lst *.pyg.sty *.pyg.vrb *.snm *.run.xml *.bbl *.blg *.nav

pdf: $(OBJECTS)

$(TARGET_DIR)/%.pdf: %.pdf
	mkdir -p $(TARGET_DIR)
	mv "$^" "$@"

%.pdf: %.tex
	$(CC) "$^" $(TEXFLAGS)

install: pdf

clean:
	for mid in $(MIDOBJS); do \
		$(RM) $$mid ; \
		$(RM) $(TARGET_DIR)/$$mid ; \
	done
	$(RM) *.pdf

uninstall: clean
	$(RM) -r $(TARGET_DIR)

rebuild: uninstall clean install
